package cn.fscode.code.generator.gen;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shenguangyang
 */
@Getter
public abstract class TableIndexSelector {
    private final GeneratorConfig generatorConfig;

    public TableIndexSelector(GeneratorConfig generatorConfig){
        this.generatorConfig = generatorConfig;
    }

    protected abstract String getTableIndexInfoSQL(String tableName);

    /**
     * 构建列信息
     */
    protected abstract List<TableIndexDefinition> buildTableIndexDefinition(List<Map<String, Object>> rowMapList);

    public List<TableIndexDefinition> getTableIndexDefinitions(String tableName) {
        List<Map<String, Object>> resultList = SqlHelper.runSql(this.getGeneratorConfig(), getTableIndexInfoSQL(tableName));

        List<TableIndexDefinition> indexDefinitionList = new ArrayList<>(resultList.size());
        // 构建indexDefinition
        indexDefinitionList.addAll(buildTableIndexDefinition(resultList));

        return indexDefinitionList;
    }
}
